﻿Imports System.Data.OleDb


Public Class JDMovieDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ImgLike.Visible = False
        Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
        Dim sqlLike As String = "SELECT * FROM Liking WHERE Member=@f1 AND FkID_Comments=@f2"
        Dim comdLike As OleDbCommand = New OleDbCommand(sqlLike, oledbConn)
        oledbConn.Open()

        comdLike.Parameters.AddWithValue("@f1", User.Identity.Name)
        comdLike.Parameters.AddWithValue("@f2", Request.Params("ID"))


        Dim Lk = comdLike.ExecuteReader()
        If Lk.HasRows Then
            'show the image
            ImgLike.Visible = True
            BtnLike.Visible = False
        Else
            'hide the image and show the button
            ImgLike.Visible = False
            BtnLike.Visible = True
        End If

        oledbConn.Close()
        
    End Sub

    Protected Sub Btn_AddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_AddComment.Click
        Dim SqlString As String = "INSERT INTO Comments(Author,Title,Comments,FkID_JDFList) VALUES (@f1,@f2,@f3,@f4)"
        Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oledbConn)
        ' cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))
        oledbConn.Open()
        cmd.ExecuteNonQuery()

        oledbConn.Close()
        cmd.Dispose()
        oledbConn.Dispose()
        Response.Redirect("JDMovieDetails.aspx?ID=" & Request.QueryString("ID"))

    End Sub

    Protected Sub Btn_AddTags_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_AddTags.Click
        Dim SqlString As String = "INSERT INTO JDFTagsJoint(FkID_JDFList, FkID_Tags) VALUES (@f1,@f2)"
        Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
        oledbConn.Open()

        For Each cb As ListItem In CbTags.Items
            If cb.Selected = True Then
                ' add this to database
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oledbConn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()

            End If
        Next


    End Sub

    Protected Sub Btn_NewTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_NewTag.Click
        ' Inserting Tag into Database
        Dim SqlString As String = "INSERT INTO Tags(TagName) VALUES (@f1)"
        Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
        oledbConn.Open()
        Dim cmdTag As OleDbCommand = New OleDbCommand(SqlString, oledbConn)
        cmdTag.Parameters.AddWithValue("@f1", tb_NewTag.Text)
        cmdTag.ExecuteNonQuery()

        'Finding the highest place in the Tags table to insert a new tag.
        Dim cmdAdd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID Desc", oledbConn)
        Dim dr As OleDbDataReader = cmdAdd.ExecuteReader
        dr.Read()
        Dim highestID = dr(0)

        'Associating newly created tag to a particular JDFMovie
        Dim associateTag As String = "INSERT INTO JDFTagsJoint(FkID_JDFList, FkID_Tags) VALUES (@f1, @f2)"
        Dim cmdAssociate As OleDbCommand = New OleDbCommand(associateTag, oledbConn)
        cmdAssociate.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmdAssociate.Parameters.AddWithValue("@f2", highestID)
        cmdAssociate.ExecuteNonQuery()


    End Sub

    Protected Sub BtnLike_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnLike.Click
        ImgLike.Visible = True
        BtnLike.Visible = False

        Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
        Dim Likes As String = "INSERT INTO Liking(FkID_Comments,Member) VALUES (@f1, @f2)"
        Dim cmdLike As OleDbCommand = New OleDbCommand(Likes, oledbConn)
        oledbConn.Open()
        cmdLike.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmdLike.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmdLike.ExecuteNonQuery()
        oledbConn.Close()
        cmdLike.Dispose()
        oledbConn.Dispose()



    End Sub
End Class